package com.wish.housing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.common.core.domain.R;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.common.security.utils.SecurityUtils;
import com.wish.housing.entity.*;
import com.wish.housing.entity.centralization.Loudong;
import com.wish.housing.service.*;
import com.wish.system.api.RemoteFileService;
import com.wish.system.api.domain.SysFile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 曹佳林 王泓翔-房源表 	！房态-杨子强 前端控制器
 * </p>
 *
 * @author gaoluwen
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/tb-house-info")
public class TbHouseInfoController {
    @Autowired
    ITbHouseInfoService iTbHouseInfoService;
    @Autowired
    ITbRelationHousePictureService iTbRelationHousePictureService;
    @Autowired
    ITbRoomInfoService iTbRoomInfoService;
    @Autowired
    ITbHouseStyleService iTbHouseStyleService;
    @Autowired
    ITbRelationStylePictureService iTbRelationStylePictureService;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    RemoteFileService remoteFileService;
    @Autowired
    ITbPremisesInfoService iTbPremisesInfoService;
    @Autowired
    ITbRelationRoomPictureService iTbRelationRoomPictureService;
    //分散式房源 首先在houseinfo 表中添加 并向 houseimage表中添加图片
    //@RequiresPermissions("housing:distributed:index")
    @PostMapping
    public MsgResult saveTbHouseInfo(@RequestBody TbHouseInfo tbHouseInfo){
        //添加 tb_house_info
        tbHouseInfo.setId(UUID.randomUUID().toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tbHouseInfo.setCreateTime(sdf.format(new Date()));
        iTbHouseInfoService.saveTbHouseInfo(tbHouseInfo);
        //添加图片 tb_relation_house_picture
        List<String> houseImages = tbHouseInfo.getHouseImages();
        for (String houseImage : houseImages) {
            TbRelationHousePicture tbRelationHousePicture = new TbRelationHousePicture(tbHouseInfo.getId(),houseImage);
            iTbRelationHousePictureService.save(tbRelationHousePicture);
        }
        Date date = new Date();
        //添加tb_room_info
        for (TbRoomInfo room : tbHouseInfo.getRoomInfoList()) {
            room.setHouseId(tbHouseInfo.getId());
            if(tbHouseInfo.getReserve1()==0){
                //给room附上  房间配套1,2tbHouseInfo = {TbHouseInfo@17778} tbHouseInfo = {TbHouseInfo@17778}
                room.setPrivateFacility(tbHouseInfo.getRoomInfoprivate());
            }                                                //houseImages 房子图片list
            iTbRoomInfoService.saveTbRoomReservation(room,tbHouseInfo.getHouseImages(),tbHouseInfo.getRoomImages());
        }
        return MsgResult.succeed(tbHouseInfo);
    }



    /*@PostMapping("uploadfile")
    public MsgResult uploadfile(@RequestParam("file") MultipartFile[] files) throws IOException {
        String path = "D://pic";
        String[] fileNames = new String[files.lstatus = {Integer@17799} 0ength];
        String img = "";
        for (int i = 0; i < files.length; i++) {
            fileNames[i] = UUID.randomUUID().toString()+files[i].getOriginalFilename();
            File file = new File(path, fileNames[i]);

            files[i].transferTo(file);
            img += "," + fileNames[i];
        }
        img = img.substring(1);
        return MsgResult.succeed(img);

    }*/
    //图片上传
    @PostMapping("uploadfile")
    public MsgResult uploadfile2(@RequestParam("file") MultipartFile file) throws Exception {
        /*String path = "D://pic";
        String fileName = "";
        String img = "";
        fileName = UUID.randomUUID().toString()+file.getOriginalFilename();
        File file1 = new File(path, fileName);
        file.transferTo(file1);*/
        R<SysFile> upload = remoteFileService.upload(file);
        String url = upload.getData().getUrl();
        return MsgResult.succeed(url);
    }
    //添加集中式房源1
    @PostMapping("saveTbHouseInfoTwo")
    public MsgResult saveTbHouseInfoTwo(@RequestBody TbHouseInfo tbHouseInfo){
        tbHouseInfo.setId(UUID.randomUUID().toString());
        tbHouseInfo.setCreateBy(SecurityUtils.getUserId()+"");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tbHouseInfo.setCreateTime(sdf.format(new Date()));
        tbHouseInfo.setPublicFacility(String.join(",", tbHouseInfo.getPublicFacilitys()));
        tbHouseInfo.setStatus(0);// 删除状态 0未删除 1 删除
        tbHouseInfo.setPublishStatus(0);// 发布状态 0未发布 1 发布
        tbHouseInfo.setType(1);// 房产类型 0 分散式 1 集中式
        //tbHouseInfo.setDeptId(super.getDeptId());
        tbHouseInfo.setManager(tbHouseInfo.getManager());
        //添加tb_house_Info
        iTbHouseInfoService.saveTbHouseInfo(tbHouseInfo);
        //添加图片 tb_relation_house_picture
        for (String houseImage : tbHouseInfo.getHouseImages()) {
            TbRelationHousePicture tbRelationHousePicture = new TbRelationHousePicture(tbHouseInfo.getId(),houseImage);
            iTbRelationHousePictureService.save(tbRelationHousePicture);
        }

        //向房源图片表添加数据
        //houseStyleService.save(houseInfo);
        List<TbHouseStyle> houseStyles = tbHouseInfo.getHouseStyles();
        for (TbHouseStyle houseStyle : houseStyles) {
            houseStyle.setId(UUID.randomUUID().toString());
            houseStyle.setHouseId(tbHouseInfo.getId());
            houseStyle.setPrivateFacility(String.join(",", houseStyle.getPrivateFacilitys()));
            //添加tb_house_style
            iTbHouseStyleService.save(houseStyle);
            for (String url : houseStyle.getRoomImages()) {
                TbRelationStylePicture tbRelationStylePicture = new TbRelationStylePicture(houseStyle.getId() + "", url);
                //添加 tb_relation_style_picture
                iTbRelationStylePictureService.save(tbRelationStylePicture);
            }
        }
        TbHouseInfo tbHouseInfo1 = iTbHouseInfoService.getHouseInfo(tbHouseInfo.getId());
        return MsgResult.succeed(tbHouseInfo1);
    }

    //保存集中式房源2
    @PostMapping("saveCentralLized")
    public MsgResult saveCentralLized(@RequestBody List<Loudong> loudongs){
        iTbRoomInfoService.saveCentralLized(loudongs);
        return MsgResult.succeed();
    }

    //根据houseId获取分散式房源信息
    @PostMapping("getDistributedHouseIfOne")
    public MsgResult getDistributedHouseIfOne(@RequestBody TbHouseInfo tbHouseInfo){
        //1.根据houseId获取tb_house_info
        TbHouseInfo houseInfo = iTbHouseInfoService.getById(tbHouseInfo.getId());
        houseInfo.setPremisesName(tbHouseInfo.getPremisesName());
        //2.根据houseId获取tb_relation_house_picture
        List<String> houseImages = iTbRelationHousePictureService.getRoomPictureByHouseId(tbHouseInfo.getId());
        houseInfo.setHouseImages(houseImages);
        //3.根据houseId获取tb_room_info
        QueryWrapper<TbRoomInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(tbHouseInfo.getId())){
            queryWrapper.eq("house_id",tbHouseInfo.getId());
        }
        List<TbRoomInfo> roomInfoList = iTbRoomInfoService.list(queryWrapper);
        //4.根据roomInfo的id 查询
        for (TbRoomInfo roomInfo : roomInfoList) {
            //iTbRelationRoomPictureService
            List<String> roomImages = iTbRelationRoomPictureService.getRoomPictureByRid(roomInfo.getId());
            roomInfo.setRoomImages(roomImages);
        }
        houseInfo.setRoomInfoList(roomInfoList);
        return MsgResult.succeed(houseInfo);
    }

}
